package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.OrderRelationQuery;
import com.coffee.order.api.sku.result.OrderRelationResult;
import com.coffee.order.api.sku.service.OrderRelationService;
import com.coffee.order.service.sku.bean.OrderRelation;
import com.coffee.order.service.sku.dao.OrderRelationDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class OrderRelationServiceImpl implements OrderRelationService {
	@Resource
	private OrderRelationDao	orderRelationDao;

	@Override
	public Page<OrderRelationResult> getPager(OrderRelationQuery param, PagerInfo pr) {
		Page<OrderRelationResult> pager=new Page<OrderRelationResult>(pr.getStart(), pr.getPageSize());
		Integer count=orderRelationDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<OrderRelation> list=orderRelationDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, OrderRelationResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(OrderRelationQuery param){
		Integer count=orderRelationDao.count(param);
		return count;
	}
	
	@Override
	public OrderRelationResult getDetail(Integer id){
		OrderRelation item=orderRelationDao.findById(id);
		if(item==null){
			return null;
		}
		OrderRelationResult result=new OrderRelationResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<OrderRelationResult> getList(OrderRelationQuery param, PagerInfo pr){
		List<OrderRelation> list=orderRelationDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, OrderRelationResult.class);
	}
	
	@Override
	public List<OrderRelationResult> getList(OrderRelationQuery param){
		List<OrderRelation> list=orderRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, OrderRelationResult.class);
	}
	
	@Override
	public List<OrderRelationResult> getList(){
		OrderRelationQuery param = new OrderRelationQuery();
		List<OrderRelation> list=orderRelationDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, OrderRelationResult.class);
	}


	@Override
	public void delete(Integer id) {
		orderRelationDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(OrderRelationResult result)throws BusinessException {
		OrderRelation item=new OrderRelation();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			orderRelationDao.insert(item);
		}else {
			OrderRelation tmp=orderRelationDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			orderRelationDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,OrderRelationResult> getItemMap(Set<Integer> idSet){
		Map<Integer, OrderRelationResult> map=new HashMap<Integer, OrderRelationResult>();
		List<OrderRelation> list=orderRelationDao.findByIds(StringUtil.setToList(idSet));
		List<OrderRelationResult> resultList=DTOUtils.convertList(list, OrderRelationResult.class);
		for(OrderRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,OrderRelationResult> getItemMap(){
		Map<Integer, OrderRelationResult> map=new HashMap<Integer, OrderRelationResult>();
		OrderRelationQuery param=new OrderRelationQuery();
		List<OrderRelation> list=orderRelationDao.page(param, 0, Integer.MAX_VALUE);
		List<OrderRelationResult> resultList=DTOUtils.convertList(list, OrderRelationResult.class);
		for(OrderRelationResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
